import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedisDemoZSet {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()){
            //test1(jedis);
            //test2(jedis);
            //test3(jedis);
            test4(jedis);
        }
    }

    private static void test4(Jedis jedis){
        System.out.println("zscore zrank");
        jedis.flushAll();
        jedis.zadd("key1", 100.0, "zhangsan");
        jedis.zadd("key1", 99.0, "lishi");
        jedis.zadd("key1", 95.0, "wangwu");
        Double result = jedis.zscore("key1", "zhangsan");
        long result2 = jedis.zrank("key1", "zhangsan");
        System.out.println(result);
        System.out.println(result2);
    }

    private static void test3(Jedis jedis) {
        System.out.println("zrem");
        jedis.flushAll();
        jedis.zadd("key1", 100.0, "zhangsan");
        jedis.zadd("key1", 99.0, "lishi");
        jedis.zadd("key1", 95.0, "wangwu");
        long result = jedis.zrem("key1", "zhangsan");
        long size = jedis.zcard("key1");
        System.out.println(result);
        System.out.println(size);
    }

    private static void test2(Jedis jedis) {
        System.out.println("zcard");
        jedis.flushAll();
        jedis.zadd("key1", 100.0, "zhangsan");
        jedis.zadd("key1", 99.0, "lishi");
        jedis.zadd("key1", 95.0, "wangwu");
        long size = jedis.zcard("key1");
        System.out.println(size);
    }

    private static void test1(Jedis jedis) {
        System.out.println("zadd zrange");
        jedis.flushAll();
        Map<String, Double> map = new HashMap<>();
        map.put("zhangsan", 100.0);
        map.put("lishi", 96.1);
        map.put("wangwu", 99.0);
        jedis.zadd("key1", map);
        List<String> list = jedis.zrange("key1", 0, -1);
        System.out.println(list);
        List<Tuple> list1 = jedis.zrangeWithScores("key1", 0, -1);
        System.out.println(list1);
        String name = list1.get(0).getElement();
        System.out.println(name);
        Double score = list1.get(0).getScore();
        System.out.println(score);
    }
}
